package com.master.train.business.controller;

import com.master.train.business.entity.TrainCarriageEntity;
import com.master.train.business.enums.SeatColEnum;
import com.master.train.business.service.TrainCarriageService;
import com.master.train.common.utils.PageUtils;
import com.master.train.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 火车车厢
 *
 * @author master
 * @email 3221057417@qq.com
 * @date 2025-07-17 16:17:17
 */
@RestController
@RequestMapping("business/admin/train-carriage")
public class TrainCarriageController {
    @Autowired
    private TrainCarriageService trainCarriageService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = trainCarriageService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		TrainCarriageEntity trainCarriage = trainCarriageService.getById(id);

        return R.ok().put("trainCarriage", trainCarriage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TrainCarriageEntity trainCarriage){
        List<SeatColEnum> colsByType = SeatColEnum.getColsByType(trainCarriage.getSeatType());
        trainCarriage.setColCount(colsByType.size());
        trainCarriage.setSeatCount(trainCarriage.getColCount() * trainCarriage.getRowCount());
        Date now = new Date();
        if (trainCarriage.getId() == null){
            trainCarriage.setCreateTime(now);
            trainCarriage.setUpdateTime(now);
        }else {
            trainCarriage.setUpdateTime(now);
        }
		trainCarriageService.saveOrUpdate(trainCarriage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TrainCarriageEntity trainCarriage){
		trainCarriageService.updateById(trainCarriage);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		trainCarriageService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
